package com.borball.store.ui.print2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

public class Print01 {
	private static final long serialVersionUID = -5124405479024666344L;
	private JPanel contentPane;
	private JTable table;
	private JFrame f;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Print01 fr = new Print01();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Print01() {
		f = new JFrame();
		f.setTitle("\u6253\u5370\u6D4B\u8BD5\u7A97\u53E3");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		f.setContentPane(contentPane);

		final JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel label = new JLabel("New label");
		label.setBounds(330, 11, 73, 15);
		panel.add(label);

		JButton button = new JButton("New button");
		button.setBounds(144, 7, 138, 23);
		panel.add(button);

		JButton button_3 = new JButton("\u6253\u5370");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// Toolkit kit = Toolkit.getDefaultToolkit();
				// Properties props = new Properties();
				// props.put("awt.print.printer", "durango");
				// props.put("awt.print.numCopies", "2");

				PrinterJob printJob = PrinterJob.getPrinterJob();
				PageFormat pageFormat = printJob.defaultPage(); // 获取默认打印页面格式
				Printable printable = new Printable() {

					public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
						if (pageIndex == 1)
							return Printable.NO_SUCH_PAGE;
						panel.printAll(graphics);
						return Printable.PAGE_EXISTS;
					}
				};
				printJob.setPrintable(printable, pageFormat); // 设置打印工作
				try {
					printJob.print();
				} catch (PrinterException e1) {
					System.out.println("错误：" + e1);
				}

			}
		});
		button_3.setBounds(340, 36, 93, 39);
		panel.add(button_3);

		JButton button_2 = new JButton("New button");
		button_2.setBounds(21, 2, 93, 30);
		panel.add(button_2);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(33, 61, 285, 168);
		panel.add(scrollPane);

		table = new JTable(9, 5);
		scrollPane.setViewportView(table);

		f.setVisible(true);
	}
}